package org.myy.learn.hot.finish.栈_1;

import java.util.Deque;
import java.util.LinkedList;

public class 最小栈 {
    Deque<Integer> deque;
    /**
     * 辅助栈
     */
    Deque<Integer> minDeque;

    /**
     * 栈初始化
     */
    public 最小栈() {
        deque = new LinkedList();
        minDeque = new LinkedList();
        minDeque.push(Integer.MAX_VALUE);
    }

    /**
     * 每个元素对应的最小值
     */
    public void push(int val) {
        deque.push(val);
        minDeque.push(Math.min(val, minDeque.peek()));
    }

    /**
     * 弹出两个栈对应元素[倒排]
     */
    public void pop() {
        deque.pop();
        minDeque.pop();
    }

    public int top() {
        return deque.peek();
    }

    public int getMin() {
        return minDeque.peek();
    }

    public static void main(String[] args) {
        最小栈 stack = new 最小栈();
        stack.push(-2);
        stack.push(0);
        stack.push(-3);
        System.out.println(stack.getMin());
        stack.pop();
        System.out.println(stack.top());
        System.out.println(stack.getMin());

    }
}
